<template>
  <scroll-view scroll-y="true" class="message-list">
    <view class="message-item" v-for="(message,index) in messageList" :key="index" :class="{'user-message':message.isUser}">
      <template v-if="!message.isUser">
        <image class="avatar" :src="message.ava" mode="aspectFill"></image>
        <view class="message-bubble">
          <text class="sender-name">{{message.title}}</text>
          <text class="message-text">{{message.text}}</text>
        </view>
      </template>
      <template v-else>
        <image class="avatar" :src="message.ava" mode="aspectFill"></image>
        <view class="message-bubble user-bubble">
          <text class="message-text">{{message.text}}</text>
        </view>
      </template>
    </view>
  </scroll-view>
  <view class="input-bar">
    <input 
      placeholder="输入消息" 
      class="input-box" 
      :value="modelValue" 
      @input="handleInput"
    >
    <button class="send-btn" @click="handleSend">发送</button>
  </view>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue'

// 定义 props
const props = defineProps({
  modelValue: String, // v-model 绑定的属性
  messageList: Array  //messageList
})

// 定义 emits
const emit = defineEmits(['update:modelValue', 'send'])

// 处理输入框变化
const handleInput = (e) => {
  emit('update:modelValue', e.detail.value)
}

// 处理发送按钮点击
const handleSend = () => {
  emit('send', props.modelValue) // 触发 send 事件
}
</script>

<style>
.chat-container {
  background: linear-gradient(165deg, #f6fbf8, #e8f5f0);
  height: 100vh;
  position: relative;
}

.message-list {
  padding: 30rpx 30rpx 160rpx;
  box-sizing: border-box;
  height: 100%;
}

.message-item {
  display: flex;
  margin-bottom: 30rpx;
  animation: fadeIn 0.5s ease forwards;
}

@keyframes fadeIn {
  from { 
    opacity: 0;
    transform: translateY(10rpx);
  }
  to { 
    opacity: 1;
    transform: translateY(0);
  }
}

.message-bubble {
  background: #ffffff;
  border-radius: 18rpx;
  max-width: 70%;
  padding: 20rpx 24rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  position: relative;
  margin-left: 20rpx;
}

.message-bubble::before {
  content: '';
  position: absolute;
  left: -10rpx;
  top: 16rpx;
  width: 0;
  height: 0;
  border-top: 12rpx solid transparent;
  border-right: 12rpx solid #ffffff;
  border-bottom: 12rpx solid transparent;
}

.user-message {
  flex-direction: row-reverse;
}

.user-bubble {
  background: linear-gradient(145deg, #27ae60, #2ecc71);
  color: white;
  line-height: 1.5;
  margin-right: 20rpx;
  margin-left: 0;
  box-shadow: 0 4rpx 12rpx rgba(39, 174, 96, 0.2);
}

.user-bubble::before {
  left: auto;
  right: -10rpx;
  border-right: none;
  border-left: 12rpx solid #2ecc71;
}

.avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  object-fit: cover;
  border: 3rpx solid #ffffff;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.sender-name {
  display: block;
  padding: 0 0 8rpx;
  font-size: 28rpx;
  font-weight: 600;
  color: #27ae60;
}

.message-text {
  font-size: 30rpx;
  line-height: 1.5;
  color: #2c3e50;
}

.user-bubble .message-text {
  color: #ffffff;
}

.input-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  display: flex;
  padding: 20rpx 30rpx;
  background: #ffffff;
  border-top: 1rpx solid rgba(39, 174, 96, 0.1);
  gap: 20rpx;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  box-sizing: border-box;
  z-index: 10;
}

.input-box {
  flex: 1;
  height: 80rpx;
  padding: 0 32rpx;
  border-radius: 40rpx;
  background: #f6fbf8;
  font-size: 30rpx;
  border: 1rpx solid rgba(39, 174, 96, 0.2);
  transition: all 0.3s ease;
}

.input-box:focus {
  box-shadow: 0 0 0 2rpx rgba(39, 174, 96, 0.1);
  background: #ffffff;
}

.send-btn {
  background: linear-gradient(145deg, #27ae60, #2ecc71);
  color: white;
  padding: 0 40rpx;
  border-radius: 40rpx;
  height: 80rpx;
  line-height: 80rpx;
  font-size: 30rpx;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 12rpx rgba(39, 174, 96, 0.2);
  transition: all 0.3s ease;
}

.send-btn:active {
  transform: translateY(2rpx);
  background: linear-gradient(145deg, #219654, #25a65c);
}
</style>